# -*- coding: utf-8 -*-
"""
@Time ： 2021/12/9 23:19
@Auth ： 颜
@File ：page_utils.py
@IDE ：PyCharm
"""
# from urllib import urlencode
from urllib.parse import urlencode


class Pagination(object):
    def __init__(self, current_page, total_count, base_url, params, per_page_count=10, max_pager_count=11):
        try:
            current_page = int(current_page)
        except Exception as e:
            current_page = 1
        if current_page <= 0:
            current_page = 1
        self.current_page = current_page
        self.total_count = total_count
        self.per_page_count = per_page_count
        max_page_num, div = divmod(total_count, per_page_count)
        if div:
            max_page_num += 1
        self.max_page_num = max_page_num
        self.max_pager_count = max_pager_count
        self.half_max_pager_count = int((max_pager_count - 1) / 2)
        self.base_url = base_url
        import copy
        params = copy.deepcopy(params)
        get_dict = params.to_dict()
        self.params = get_dict

    @property
    def start(self):
        return (self.current_page - 1) * self.per_page_count

    @property
    def end(self):
        return self.current_page * self.per_page_count

    def page_html(self):
        if self.max_page_num <= self.max_pager_count:
            pager_start = 1
            pager_end = self.max_page_num
        # 如果总页数 > 11
        else:
            if self.current_page <= self.half_max_pager_count:
                pager_start = 1
                pager_end = self.max_pager_count
            else:
                if (self.current_page + self.half_max_pager_count) > self.max_page_num:
                    pager_end = self.max_page_num
                    pager_start = self.max_page_num - self.max_pager_count+1   # 倒这数11个
                else:
                    pager_start = self.current_page - self.half_max_pager_count
                    pager_end = self.current_page + self.half_max_pager_count
        page_html_list = []
        self.params['page'] = 1
        first_page = '<span class="prev" style="margin-right: 20px;"><a href="%s?%s">首页</a></span>'.encode('utf-8').decode("utf-8") % (
            self.base_url, urlencode(self.params),)
        page_html_list.append(first_page)
        self.params["page"] = self.current_page - 1
        if self.params["page"] < 1:
            pervious_page = '<span class="prev" style="margin-right: 20px;"><a href="%s?%s" aria-label="Previous">上一页</a></span>'.encode(
                'utf-8').decode(
                "utf-8") % (self.base_url, urlencode(self.params))
        else:
            pervious_page = '<span class="prev" style="margin-right: 20px;"><a href = "%s?%s" aria-label = "Previous" >上一页</a></span>'.encode(
                'utf-8').decode(
                "utf-8") % (
                                self.base_url, urlencode(self.params))
        page_html_list.append(pervious_page)
        # 中间页码
        for i in range(pager_start, pager_end):
            self.params['page'] = i
            if i == self.current_page:
                temp = '<a href="%s?%s" style="margin-left: 20px;">%s</a>' % (self.base_url, urlencode(self.params), i,)
            else:
                temp = '<a href="%s?%s" style="margin-left: 20px;">%s</a>' % (self.base_url, urlencode(self.params), i,)
            page_html_list.append(temp)
        self.params["page"] = self.current_page + 1
        if self.params["page"] > self.max_page_num:
            self.params["page"] = self.current_page
            next_page = '<span class="next" style="padding: 0;margin-left: 20px;"><a href = "%s?%s" aria-label = "Next">下一页</a></span>'.encode(
                'utf-8').decode("utf-8") % (self.base_url, urlencode(self.params))
        else:
            next_page = '<span class="next" style="padding: 0;margin-left: 20px;"><a href = "%s?%s" aria-label = "Next">下一页</a></span>'.encode(
                'utf-8').decode(
                "utf-8") % (
                            self.base_url, urlencode(self.params))
        page_html_list.append(next_page)
        self.params['page'] = self.max_page_num
        last_page = '<span class="prev" style="margin-left: 20px;font-size: 14px;"><a href="%s?%s">尾页</a></span>'.encode('utf-8').decode("utf-8") % (
            self.base_url, urlencode(self.params),)
        page_html_list.append(last_page)
        return ''.join(page_html_list)
